package com.fhz.shop.ui.dashboard.right;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fhz.shop.R;
import com.fhz.shop.base.BaseFragment;
import com.fhz.shop.callback.ApiResponse;
import com.fhz.shop.databinding.FragmentHomeBinding;
import com.fhz.shop.databinding.FragmentRightBinding;
import com.fhz.shop.entity.Category;
import com.fhz.shop.ui.dashboard.DashboardViewModel;
import com.fhz.shop.ui.home.HomeViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class RightFragment extends BaseFragment<DashboardViewModel, FragmentRightBinding> {


    DashRightRecyclerViewAdapter mDashRightRecyclerViewAdapter;
    List<Category> mCategoryList;
    @Override
    protected void initData() {


        setAdapter();


        viewModel.getCategory(1).observe(this, new Observer<ApiResponse<List<Category>>>() {
            @Override
            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                mCategoryList.addAll(listApiResponse.getData());
                mDashRightRecyclerViewAdapter.notifyDataSetChanged();
            }
        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Integer i) {
        // Do something
        viewModel.getCategory(i+1).observe(this, new Observer<ApiResponse<List<Category>>>() {
            @Override
            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                mCategoryList.clear();
                mCategoryList.addAll(listApiResponse.getData());
                mDashRightRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
    }



    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void setAdapter() {
        mCategoryList = new ArrayList<>();
        mDashRightRecyclerViewAdapter = new DashRightRecyclerViewAdapter(getContext(),mCategoryList);
        binding.rightRecyclerView.setAdapter(mDashRightRecyclerViewAdapter);
        binding.rightRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_right;
    }

    @Override
    protected DashboardViewModel createViewModel() {
        return new ViewModelProvider(getParentFragment()).get(DashboardViewModel.class);
    }
}